

package com.smalls.redshoes.ui.orderstate;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.smalls.newvideotwo.R;
import com.smalls.newvideotwo.util.LogUtil;
import com.smalls.newvideotwo.utils.Constant;

import java.util.List;

public class OrderStateAdapter extends ArrayAdapter<OrderState> {
    private int resourceId;
    private static String TAG = Constant.baddcoolio2tag?Constant.TAGPREFIXSTR + OrderStateAdapter.class.getSimpleName():OrderStateAdapter.class.getSimpleName();

    // 适配器的构造函数，把要适配的数据传入这里
    public OrderStateAdapter(Context context, int textViewResourceId, List<OrderState> objects){
        super(context,textViewResourceId,objects);
        resourceId=textViewResourceId;
    }

    // convertView 参数用于将之前加载好的布局进行缓存
    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        OrderState order=getItem(position); //获取当前项的Fruit实例
        LogUtil.d(TAG,"onActivityResult order:" + order.toString() + ",id:"+order.getId());
        // 加个判断，以免ListView每次滚动时都要重新加载布局，以提高运行效率
        View view;
        ViewHolder viewHolder;
        if (convertView==null){

            // 避免ListView每次滚动时都要重新加载布局，以提高运行效率
            view=LayoutInflater.from(getContext()).inflate(resourceId,parent,false);

            // 避免每次调用getView()时都要重新获取控件实例
            viewHolder=new ViewHolder();
            viewHolder.orderid=(TextView)view.findViewById(R.id.order_id);
            viewHolder.orderdatetime=(TextView)view.findViewById(R.id.order_datetime);
            viewHolder.orderstate=(TextView)view.findViewById(R.id.order_state);


            // 将ViewHolder存储在View中（即将控件的实例存储在其中）
            view.setTag(viewHolder);
        } else{
            view=convertView;
            viewHolder=(ViewHolder) view.getTag();
        }

        // 获取控件实例，并调用set...方法使其显示出来
        viewHolder.orderid.setText(order.getId());
        viewHolder.orderdatetime.setText(order.getDateTime());
        viewHolder.orderstate.setText(order.getState());
        return view;
    }

    // 定义一个内部类，用于对控件的实例进行缓存
    class ViewHolder{
        TextView orderid;
        TextView orderdatetime;
        TextView orderstate;
    }
}